from metagpt.actions import Action
from metagpt.logs import logger
import time
from datetime import datetime
import requests

class AnnounceError(Action):
    name: str = "AnnounceError"

    async def run(self, filenames: str):
        resp = "The code translation has some errors, we now working on debugging it"
        timestamp = time.time()
        # 将秒数转换为日期和时间
        dt_object = datetime.fromtimestamp(timestamp)
        # 将日期和时间转换为字符串
        human_readable_time = dt_object.strftime("%Y-%m-%d %H:%M:%S")
        data = {"time": human_readable_time,"compare":timestamp,"sender":"code_reviewer","receiver":"code_corrector","content":resp}
        response = requests.post("http://localhost:5400/post_msg",json=data)
        response_data = response.json()
        print(response_data)

        return filenames
    
class AnnounceSuccess(Action):
    name: str = "AnnounceSuccess"

    async def run(self):
        resp = "Congratulations! There is no error in compiler's messages."
        timestamp = time.time()
        # 将秒数转换为日期和时间
        dt_object = datetime.fromtimestamp(timestamp)
        # 将日期和时间转换为字符串
        human_readable_time = dt_object.strftime("%Y-%m-%d %H:%M:%S")
        data = {"time": human_readable_time,"compare":timestamp,"sender":"code_reviewer","receiver":"code_corrector","content":resp}
        response = requests.post("http://localhost:5400/post_msg",json=data)
        response_data = response.json()
        print(response_data)
        return "Congratulations! There is no error in compiler's messages."

class AnnounceEnd(Action):
    name: str = "AnnounceEnd"
    
    async def run(self):
        return "The code translation failed as we have reached the limit of iterations."
